package com.lzhl.buildjar.ui;

import com.lzhl.buildjar.App;
import com.lzhl.buildjar.exception.AbortException;
import com.lzhl.buildjar.ui.support.Cmd;
import com.lzhl.buildjar.ui.support.CmdListener;
import com.lzhl.buildjar.util.UIUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2015/11/25
 */
public class MainUI extends JFrame implements CmdListener, Thread.UncaughtExceptionHandler{

    private CategoryTreeUI tree;

    public MainUI() {
        super("打包");
        Image logo = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/image/logo.png"));
        setIconImage(logo);

        jbinit();
        init();
    }

    public void jbinit() {
        setLayout(new BorderLayout());

        //工具栏
        ToolBarUI toolBar = new ToolBarUI();
        add(toolBar, BorderLayout.NORTH);

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setDividerLocation(180);
        add(splitPane, BorderLayout.CENTER);

        //方案栏
        tree = new CategoryTreeUI();
        JScrollPane spTree = new JScrollPane(tree);
        splitPane.setLeftComponent(spTree);

        //编辑栏
        ClassItemUI classItemUI = new ClassItemUI();
        splitPane.setRightComponent(classItemUI);

        //获取屏幕分辨率大小
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        //getScreenInsets是指获得屏幕的 insets
        Insets inset = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
        int w = (int) ((screen.width - inset.left - inset.right) * 0.9);
        if(w > 1600) {
            w = 1600;
        }
        int h = (int) ((screen.height - inset.top - inset.bottom) * 0.8);
        if(h > 1200) {
            h = 1200;
        }
        setSize(w, h);

        setLocationRelativeTo(null);
    }

    public void init() {
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                exit();
            }
        });

        Thread.setDefaultUncaughtExceptionHandler(this);

        App.registCmdListener(this);

//        UIUtils.showOwnerFocus(this);
    }

    @Override
    public Object handleCmd(Cmd cmd, Object data) {
        if(Cmd.LOGOUT.equals(cmd)) {
            exit();
        }
        if(Cmd.TOOLS.equals(cmd)) {
            ToolUI.show(this);
        }
        return null;
    }

    public void uncaughtException(Thread t, Throwable e) {
        if(e instanceof AbortException) {
            AbortException abort = (AbortException) e;
            String message = e.getMessage();
            if(message != null) {
                JOptionPane.showMessageDialog(this, message);
            }
            if(abort.getAfter() != null) {
                abort.getAfter().run();
            }
        }
        else {
            JOptionPane.showMessageDialog(this, e.getMessage());
            e.printStackTrace();
        }
    }

    public void open() {
        setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                tree.requestFocus();
            }
        });
    }

    public void exit() {
        tree.checkSave();

        setVisible(false);
        dispose();
        System.exit(0);
    }

}
